package com.empresa.androidchkapp;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class TiposChksEmpresaActivity extends ListActivity implements
		OnItemClickListener {
	
	private ArrayList<Map<String, Object>> chksList;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//setContentView(R.layout.activity_checklists_empresa);

		Integer idDaEmpresa = null;
		Intent intent = getIntent();
		Bundle params = intent.getExtras();

		if (params != null) {
			idDaEmpresa = params.getInt("idEmpresa");
			Toast.makeText(this, idDaEmpresa.toString(), Toast.LENGTH_SHORT)
					.show();
		}
		
		String[] de = { "tipo" };
		int[] para = { R.id.tipoChk };

		SimpleAdapter adapter = new SimpleAdapter(this, listarChks(idDaEmpresa),
				R.layout.activity_tipos_chks_empresa, de, para);
		setListAdapter(adapter);
		// setListAdapter(new ArrayAdapter<Map<String, Object>>(this,
		// android.R.layout.simple_list_item_1, listarEmpresas()));

		getListView().setOnItemClickListener(this);


	}

	private List<Map<String, Object>> listarChks(Integer empresaID) {
		
		//RECUPERA USUARIO da SESSAO
		Aplicacao appState = ((Aplicacao)getApplicationContext());
		//final String UsuarioID = appState.getUsuarioLogadoId();
		final String UsuarioID = "1";
		
		
		// Fazer um select dos tipos de checklists da emmpresa selecionada
		String sql = "select distinct ChecklistId, nome from [ChecklistRoteiroes] " +
				"inner join [Checklists] on [Checklists].[_id] = [ChecklistRoteiroes].[ChecklistId] " +
				"inner join [Roteiroes] on [Roteiroes].[_id] = [ChecklistRoteiroes].[RoteiroId] " +
				"where EmpresaId = "+empresaID+" and [Roteiroes].[UsuarioId] = "+UsuarioID; 
		
		DataBaseHelper baseHelper = new DataBaseHelper(getApplicationContext());
		SQLiteDatabase db = baseHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery(sql, null);

		cursor.moveToFirst();
		chksList = new ArrayList<Map<String, Object>>();

		for (int i = 0; i < cursor.getCount(); i++) {
			Map<String, Object> item = new HashMap<String, Object>();

			int id = cursor.getInt(0);
			String tipo = cursor.getString(1);
			item.put("id", id);
			item.put("tipo", tipo);

			chksList.add(item);
			cursor.moveToNext();
		}
		cursor.close();
		db.close();
		return chksList;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.checklists_empresa, menu);
		return true;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		
		Bundle params = new Bundle();

		Map<String, Object> map = chksList.get(position);
		int idChk = (Integer) map.get("id");

		params.putInt("idChk", idChk);

		Intent intent = new Intent(this, ChkItensActivity.class);
		intent.putExtras(params);
		startActivity(intent);

	}

}
